/**
 * 
 */
package at.ac.lbg.media.vis.framework.persistence;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;

import at.ac.lbg.media.vis.framework.domain.ArtworkDO;
import at.ac.lbg.media.vis.framework.domain.ArtworkDetailsDO;

/**
 * @author Evelyn Münster
 *
 */
public class ArtworkDAO implements IArtworkDAO {
	
	private HibernateTemplate hibernateTemplate;
	protected final Log logger = LogFactory.getLog(getClass());
	List<ArtworkDO> allArtworks = null;
	
	public ArtworkDAO() {}
	
   @SuppressWarnings("unchecked")
    public List<ArtworkDO> getAllArtworks() {
        List<ArtworkDO> allArtworks = null;
        String queryString = "from ArtworkDO";
        try {
            allArtworks = (List<ArtworkDO>) hibernateTemplate.find(queryString);
        } catch (DataAccessException e) {
            logger.error("Could not load artworks: " + queryString, e);
        }
        
        return allArtworks;
    }
	
	@SuppressWarnings("unchecked")
	public List<ArtworkDO> getArtworks(long maxId) {
		List<ArtworkDO> allArtworks = null;
		String queryString = "from ArtworkDO as aw where aw.id < ?";
		try {
			allArtworks = (List<ArtworkDO>) hibernateTemplate.find(queryString, maxId);
		} catch (DataAccessException e) {
			logger.error("Could not load leafcategories: " + queryString, e);
		}
		
		return allArtworks;
	}
	
	@SuppressWarnings("unchecked")
    public ArtworkDetailsDO getArtworkDetails(long artworkId) {
        List<ArtworkDetailsDO> allArtworks = null;
        String queryString = "from ArtworkDetailsDO as aw where aw.id = ?";
        try {
        	allArtworks = (List<ArtworkDetailsDO>) hibernateTemplate.find(queryString, artworkId);
        } catch (DataAccessException e) {
            logger.error("Could not load leafcategories: " + queryString, e);
        }
        
        return allArtworks.get(0);
    }

	/**
	 * @param template the hTemplate to set
	 */
	public void setHibernateTemplate(HibernateTemplate template) {
		hibernateTemplate = template;
	}
	
	

}
